
from django.db.models import Q
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from coderedis import codeRedis
from .models import CartModel
from .serializers import CartSerializer
from rest_framework.pagination import PageNumberPagination
from drf_haystack.viewsets import HaystackViewSet
from datetime import datetime

class CartPagination(PageNumberPagination):
    page_size = 5
    page_size_query_param = 'page_size'

class CartViewSet(viewsets.ModelViewSet):
    pagination_class = CartPagination

    # 添加购物车数据
    @action(methods=['post'], detail=False)
    def cart_add(self, request):
        pass
        return Response({"code": 200, "msg": "添加成功"})

    # 查询购物车数据
    @action(methods=['get'], detail=False)
    def cart_list(self, request):
        # carts = CartModel.objects.filter(user=request.user)
        # serializer = CartSerializer(carts, many=True).data
        return Response({
            'code': 200,
            'message': 'success',
            'data': []
        })

    # 修改购物车数量和状态
    @action(methods=['put'], detail=False)
    def cart_update(self, request):
        carts = CartModel.objects.filter(user=request.user)
        serializer = CartSerializer(carts, many=True).data
        return Response({
            'code': 200,
            'message': 'success',
            'data': serializer
        })